<template>
  <view class="evaluate-core-component">
    <hsb-business-evaluate-product
      :product-info="productInfo"
      :loading="productLoading"
      :select="selectLen"
      :total="total"
      @change="$emit('change-model', $event)"
    />
    <hsb-business-evaluate-poly
      v-if="type === 'hsb'"
      ref="evInstance"
      :err-msg="errMsg"
      :option-list="optionList"
      :loading="productLoading"
			class="hsb-business-evaluate-poly"
      @change="selectData"
      @deblock="$emit('deblock', $event)"
			@icloubLock="$refs.icloubLock.open()"
			@paramsPreview="paramsPreview"
    />
    <hsb-business-evaluate-button
      v-if="canEvaluate"
      :loading="evaluateLoading"
      @evaluate="evaluate"
      @reset="$emit('reset', $event)"
    />
		<hsb-business-icloub-lock
			ref="icloubLock"
		/>
		<hsb-business-params-preview
			ref="paramsPreview"
			:info="previewInfo"
		/>
  </view>
</template>

<script>
export default {
	name: 'EvaluateCore',
	props: {
		optionList: Array,
		tvOptions: {
		  type: Object,
		  default () {
		    return {}
		  }
		},
		productInfo: Object,
		type: {
		  type: String,
		  default: 'hsb'
		},
		productAttrs: {
		  type: Object,
		  default () {
		    return {}
		  }
		},
		evaluateAttrs: {
		  type: Object,
		  default () {
		    return {}
		  }
		},
		buttonAttrs: {
		  type: Object,
		  default () {
		    return {}
		  }
		},
		productLoading: Boolean,
		evaluateLoading: Boolean,
		errMsg: String
	},
	computed: {
		canEvaluate () {
			return this.selectLen > 0 && this.selectLen === this.total
		}
	},
	data () {
		return {
			total: 0,
			selectLen: 0,
			prices: 0,
			previewInfo: {}
		}
	},
	methods: {
		selectData ({ total, selectLen, prices = 0 }) {
			this.total = total
			this.selectLen = selectLen
			this.prices = prices
		},
		paramsPreview (courseInfo) {
			this.previewInfo = courseInfo || {}
			this.$refs.paramsPreview.open()
		},
		evaluate () {
			this.$emit('evaluate', {
			  prices: this.prices,
			  selectData: this.$refs.evInstance.selectList || []
			})
		}
	}
}
</script>

<style lang="scss" scoped>
@import './index.scss';
</style>